package lab1Exercise1;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.io.Serializable;

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.util.ArrayList;
import java.util.StringTokenizer;

public class Collection implements Serializable {
	
	private ArrayList<Person> people;
	private int minAge;
	private int maxAge;
	private int totalAge;
	
	public Collection()
	{
		people = new ArrayList<Person>();
		minAge = 0;
		maxAge = 0;
		totalAge = 0;
	}
	
	public void readFromFile(String filename)
	{
		try {
			String line = "";
			StringTokenizer st;
			FileReader fr = new FileReader(filename);
			BufferedReader br = new BufferedReader(fr);
			int incr = 0;
			
			while(br.ready()) {
				line = br.readLine();
				st = new StringTokenizer(line, "; ");

				ArrayList<String> tokens = new ArrayList<String>();
				
				while(st.hasMoreTokens()) {
					tokens.add(st.nextToken().toString());
				}
				
				people.add(new Person(tokens.get(1), tokens.get(0), Integer.parseInt(tokens.get(3))));
				if(minAge == 0) {
					minAge = people.get(incr).age;
				}
				if (minAge > people.get(incr).age) {
					minAge = people.get(incr).age;
				}
				
				if(maxAge < people.get(incr).age) {
					maxAge = people.get(incr).age;
				}
				totalAge +=people.get(incr).age;
				incr++;
			}
			
			/*for(Person p : people) {
				System.out.println(p.toString());
			}*/
			
			br.close();
		} catch (Exception e) {
			System.out.println(e.getMessage());
		}
		
	}
	public void writeToFile(String filename)
	{
		try {
			FileWriter fw = new FileWriter(filename);
			BufferedWriter bw = new BufferedWriter(fw);
			StringBuffer val = new StringBuffer("Minimum age: ");
			val.append(fastMinAge());
			val.append("\nMaximum age: ");
			val.append(fastMaxAge());
			val.append("\nAverage age: ");
			val.append(fastAvgAge());
			val.append("\n");
			bw.write(val.toString());
			for(Person p : people) {
				bw.write("\n" + p.toString());
			}
			bw.close();
			System.out.println("bam");
		} 
		catch (IOException e) {
			System.out.println(e.getMessage());
		}
		
	}
	
	public int fastMinAge()
	{
		return minAge;
	}
	public int fastMaxAge()
	{
		return maxAge;
	}
	public float fastAvgAge()
	{
		return totalAge/people.size();
	}
	/*public int minAge()
	{
		int minAge = 1000;
		for(Person p : people) {
			if(p.age < minAge) {
				minAge = p.age;
				//System.out.println("Changing minAge to: "+ p.age);
			}
		}
		return minAge;
	}
	
	public int maxAge()
	{
		int maxAge = 0;
		for(Person p: people) {
			if(p.age > maxAge) {
				maxAge = p.age;
			}
		}
		return maxAge;
	}
	
	public float averageAge()
	{
		float average = 0;
		int sum = 0;
		for(Person p : people) {
			sum += p.age;
			average = sum / people.size();
		}
		return average;
	}*/
	
	public void testy()
	{
		System.out.println("Result of minAge is: "+ fastMinAge());
		System.out.println("Result of maxAge is: "+ fastMaxAge());
		System.out.println("Result of averageAge is: "+ fastAvgAge());
	}
	public void writeObject(Object object) throws IOException
	{
		FileOutputStream fos = new FileOutputStream("/home/irek/workspace/SD3/src/lab1Exercise1/objectfile");
		ObjectOutputStream oos = new ObjectOutputStream(fos);
		oos.writeObject(object);
		oos.close();
	}
	
	public void readObject() throws IOException, ClassNotFoundException
	{
		FileInputStream fis = new FileInputStream("/home/irek/workspace/SD3/src/lab1Exercise1/objectfile");
		ObjectInputStream ois = new ObjectInputStream(fis);
		ArrayList<Person> c = (ArrayList<Person>) ois.readObject();
		System.out.println(c.size());
		for(Person p : c) {
			System.out.println(p.toString());
		}
			
		ois.close();
	}
	
	public static void main(String[] args) throws IOException, ClassNotFoundException
	{
		Collection test = new Collection();
		test.readFromFile("/home/irek/workspace/SD3/src/lab1Exercise1/people.txt");
		test.writeToFile("/home/irek/workspace/SD3/src/lab1Exercise1/peoplewrite.txt");
		test.testy();
		test.writeObject(test.people);
		test.readObject();
	}

}
